package rongyao;

import java.util.Scanner;

/**
 * @author Hang
 * @date 2022/8/20
 * @project AutumnOffer
 * @description
 **/
public class Test2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String[] str = new String[n];
        for(int i=0;i<n;i++){
            str[i] = sc.next();
        }
        String[] time = new String[n];
        for(int i=0;i<n;i++){
            String[] split = str[i].split("/");
            time[i] = split[1];
        }
        for(int i=0;i<time.length-1;i++){
            for(int j=i+1;j<time.length;j++){
                if(CompareTime(time[i],time[j])>0){
                    String tmp = str[i];
                    str[i] = str[j];
                    str[j] = tmp;
                    String timeTmp = time[i];
                    time[i] = time[j];
                    time[j] = timeTmp;
                }
            }
        }
        for(int i=0;i<time.length-1;i++){
            if(CompareTime(time[i],time[i+1])!=0){
                System.out.println(str[i]);
            }else if(str[i].equals(str[i+1])){
                System.out.println(str[i]);
                i++;
            }else{
                if(CompareStr(str[i],str[i+1])<0){
                    System.out.println(str[i]);
                }else{
                    String tmp = str[i];
                    str[i] = str[i+1];
                    str[i+1] = tmp;
                    System.out.println(str[i]);
                }
            }
        }
    }
    public  static int CompareStr(String str1,String str2){
        for(int i=0;i<Math.min(str1.length(),str2.length());i++){
            if(str1.charAt(i)-str2.charAt(i)>0) return 1;
            else if(str1.charAt(i)-str2.charAt(i)<0) return -1;
            else continue;
        }
        return str1.length() == str2.length() ? 0:str1.length() >= str2.length()?1:-1;
    }
    public static int CompareTime(String str1,String str2){
        String[] ts1 = str1.split("T");
        String[] ts2 = str2.split("T");
        String[] split1 = ts1[0].split("-");
        String[] split2 = ts2[0].split("-");
        for(int j=0;j<3;j++){
            if(Integer.parseInt(split1[j])>Integer.parseInt(split2[j])) return 1;
            else if(Integer.parseInt(split1[j])<Integer.parseInt(split2[j])) return -1;
            else continue;
        }
        String[] split3 = ts1[1].split(":");
        String[] split4 = ts2[1].split(":");
        for(int j=0;j<3;j++){
            if(Integer.parseInt(split3[j])>Integer.parseInt(split4[j])) return 1;
            else if(Integer.parseInt(split3[j])<Integer.parseInt(split4[j])) return -1;
            else continue;
        }
        return 0;
    }
}
